package com.sxdx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxdx.common.Result;
import com.sxdx.entity.LoginUser;
import com.sxdx.entity.SysUser;
import com.sxdx.entity.dto.SysUserDto;
import com.sxdx.mapper.SysUserMapper;
import com.sxdx.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxdx.utils.DigestUtil;
import com.sxdx.utils.JwtUtil;
import com.sxdx.utils.RegexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.sxdx.utils.RedisConstants.LOGIN_USER_KEY;
import static com.sxdx.utils.RedisConstants.LOGIN_USER_TTL;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 靳杰
 * @since 2023-12-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private static String ADMIN="1";
    @Autowired
    private SysUserMapper sysUserMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 修改用户信息业务
     *
     * @param sysUserDto 用户信息
     * @return 结果
     */
    @Override
    public Result updateSysUser(SysUserDto sysUserDto) {
        if (ADMIN.equals(sysUserDto.getRoleId())){

        }
//        return sysUserMapper.updateSysUser(sysUserDto)== 1 ? Result.success("修改成功") : Result.error("修改失败");
        return null;
    }




    /**
     * 实现用户登入功能
     * @param user
     * @return
     */
    @Override
    public Result login(LoginUser user) {
        //1.校验验证码是否正确
//        if(Boolean.FALSE.equals(stringRedisTemplate.hasKey(user.getVerificationCode()))){
//            return Result.error("验证码错误");
//        }

        //2.校验用户名是否合法
        String username = user.getUsername();
        if (RegexUtils.isUserNameInvalid(username)){
            System.out.println(username);
            return Result.error("用户名格式错误！");
        }
        //3.检查密码是否为空
        if (StrUtil.isBlank(user.getPassword())){
            return Result.error("密码不能为空！");
        }

        //4.根据用户名查询数据库中的用户  select * from dx_sys_user where username = ?

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        SysUser dxSysUser = sysUserMapper.selectOne(wrapper);
        if (dxSysUser == null){
            return Result.error("用户不存在！");
        }
        //5.用户存在，对前段接收到的密码进行md5加密然后和数据库中的进行比较
        String password = DigestUtil.hmacSign(user.getPassword());
        if (!password.equals(dxSysUser.getPassword())){
            return Result.error("密码错误");
        }
        //6.保存用户信息到redis中
        //4.1随机生产token，作为登入令牌,使用hutool工具类提供的uuid
//        String token = UUID.randomUUID(true).toString();
        String token= JwtUtil.createToken(Integer.parseInt(dxSysUser.getUserId()));
        //4.2将user对象转为hashMap存储到redis
//        SysUserDto sysUserDto = BeanUtil.copyProperties(user, SysUserDto.class);
//        String tokenKey = LOGIN_USER_KEY + token;
//        Map<String, Object> userMap = BeanUtil.beanToMap(sysUserDto);
        stringRedisTemplate.opsForValue().set(token,dxSysUser.getUserId(),LOGIN_USER_TTL,TimeUnit.MINUTES);
        //设置redis中user对象的有效期
//        stringRedisTemplate.expire(tokenKey,LOGIN_USER_TTL, TimeUnit.MINUTES);
//        Map<String,String> data=new HashMap<>();
        Map<String,String> data=new HashMap<>();

        data.put("token",token);
        return Result.success(data);
    }
}
